package com.gpay.erp.core.system.bean;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.gpay.erp.core.dao.page.PagerModel;

/**
 * easui中的tree_data.json数据,只能有一个root节点
 * [{   
    "id":1,   
    "text":"Folder1",   
    "iconCls":"icon-save",   
    "children":[{   
        "text":"File1",   
        "checked":true  
    }]   
}] 
 * 提供静态方法formatTree(List<TreeJson> list) 返回结果
 * TreeJson.formatTree(treeJsonlist) ;
 * @author lg
 *
 */
public class TreeNode implements Serializable {

	private  String id;
	private String preId;   //父节点ID
	private String text; //节点名
	private String url;  //URL
	private String state ; 
	private Boolean checked;
	private Map<String, Object> attributes=new HashMap<String, Object>();
	private List<TreeNode> children=new ArrayList<TreeNode>() ;

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}
	public Boolean getChecked() {
		return checked;
	}
	public void setChecked(Boolean checked) {
		this.checked = checked;
	}
	public String getState() {
		return state;
	}
	public void setState(String state) {
		this.state = state;
	}
	public String getPreId() {
		return preId;
	}
	public void setPreId(String preId) {
		this.preId = preId;
	}
	public String getText() {
		return text;
	}
	public void setText(String text) {
		this.text = text;
	}
	public String getUrl() {
		return url;
	}
	public void setUrl(String url) {
		this.url = url;
	}

	public Map<String, Object> getAttributes() {
		return attributes;
	}
	public void setAttributes(Map<String, Object> attributes) {
		this.attributes = attributes;
	}
	public List<TreeNode> getChildren() {
		return children;
	}
	public void setChildren(List<TreeNode> children) {
		this.children = children;
	}
	public static List<TreeNode> formatTree(List<TreeNode> list) {

		TreeNode root = new TreeNode();
		TreeNode node = new TreeNode();		
        List<TreeNode> treelist = new ArrayList<TreeNode>();// 拼凑好的json格式的数据
        List<TreeNode> parentnodes = new ArrayList<TreeNode>();// parentnodes存放所有的父节点
        
        if (list != null && list.size() > 0) {
            root = list.get(0) ;
            root.checked=true;
            //循环遍历oracle树查询的所有节点
            for (int i = 1; i < list.size(); i++) {
                node = list.get(i);
                node.getAttributes().put("url", node.getUrl());
                if(node.getPreId().equals(root.getId())){
                	
                    //为tree root 增加子节点
                    parentnodes.add(node) ;
                    root.getChildren().add(node) ;
                }else{//获取root子节点的孩子节点
                    getChildrenNodes(parentnodes, node);
                    parentnodes.add(node) ;
                }
            }    
        }
        treelist.add(root) ;
        return treelist ;

    }
	 private static void getChildrenNodes(List<TreeNode> parentnodes, TreeNode node) {
	        //循环遍历所有父节点和node进行匹配，确定父子关系
	        for (int i = parentnodes.size() - 1; i >= 0; i--) {
	            
	        	TreeNode pnode = parentnodes.get(i);
	            //如果是父子关系，为父节点增加子节点，退出for循环
	            if (pnode.getId().equals(node.getPreId())) {
	                pnode.setState("closed") ;//关闭二级树
	                pnode.getChildren().add(node) ;
	                return ;
	            } else {
	                //如果不是父子关系，删除父节点栈里当前的节点，
	                //继续此次循环，直到确定父子关系或不存在退出for循环
	                parentnodes.remove(i) ;
	            }
	        }
	    }
	
}
